package cn.dida.controller;

import cn.dida.exception.BizzException;
import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.model.LgsCust;
import cn.dida.model.Pageable;
import cn.dida.model.UserSession;
import cn.dida.service.LgsCustService;
import cn.dida.util.ValidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by zhangzhiling on 2017/4/21.
 */
@Controller
//@RequestMapping("/lgsCust")
public class LgsCustController extends BaseController{

    @Autowired
    private LgsCustService lgsCustService;


    /**************************** 对象管理 ****************************/

    /**
     * 获取分页列表信息
     * @param request
     * @param keyword
     * @param state
     * @param page
     * @return
     */
    @GetMapping("/lgsCust/list")
    public ModelAndView query(HttpServletRequest request, String keyword, Integer state, Integer page){
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        page = (page == null || page < 1) ? 1 : page;
        keyword = StringUtils.trimToNull(keyword);
        Integer companyId = userSession.getCompanyId();
        Pageable<LgsCust> list = lgsCustService.query(companyId,keyword,state,page);

        List<Map<String,Object>> areaList = lgsCustService.getAreaList(companyId);

        model.put("custList", list);
        model.put("keyword", keyword);
        model.put("state", state);
        model.put("areaList",areaList);
        return new ModelAndView("lgs_cust").addAllObjects(model);
    }

    /**
     * 添加
     * @param request
     * @param lgsCust
     * @return
     */
    @ResponseBody
    @PostMapping("/lgsCust/postLgsCust")
    public HttpResponseBodyWrapper setDepot(HttpServletRequest request, @RequestBody LgsCust lgsCust){

        if(lgsCust == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入参数！");
        }
        String custName = lgsCust.getCustName();
        String custCode = lgsCust.getCustCode();
        String addr = lgsCust.getAddr();
        Integer areaId = lgsCust.getAreaId();
        lgsCust.setState(lgsCust.getState() == null ? 1 : lgsCust.getState());

        if(custName == null || custName.equals("")){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入客户名称！");
        }
        if(!Pattern.matches(ValidUtil.WENZI_PATTERN,custName)){
            throw new BizzException(BizzException.error_code_invalid_params, "客户名称仅限汉字、数字、字母！");
        }
        if(custCode == null || custCode == ""){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入客户编码！");
        }
        if(!Pattern.matches(ValidUtil.PWS_PATTERN,custCode)){
            throw new BizzException(BizzException.error_code_invalid_params, "客户编码仅限数字、字母！");
        }
        if(addr == null || addr.equals("")){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入详细地址！");
        }
        if(areaId == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请选择区域！");
        }

        UserSession userSession = getUserSession(request);
        Integer companyId = userSession.getCompanyId();
        if(lgsCustService.testCode(companyId,custCode) != null ){
            throw new BizzException(BizzException.error_code_invalid_params, "客户编码已存在！");
        }
        lgsCust.setCustProp(1);
        lgsCust.setCreatorName(userSession.getId_admin());
        lgsCust.setCompanyId(userSession.getCompanyId());
        lgsCust.setSource(1);
        Boolean result = lgsCustService.insert(lgsCust);
        return new HttpResponseBodyWrapper(result);
    }

    /**
     * 修改
     * @param request
     * @param lgsCust
     * @return
     */
    @ResponseBody
    @PostMapping("/lgsCust/update")
    public HttpResponseBodyWrapper update(HttpServletRequest request, @RequestBody LgsCust lgsCust){

        if(lgsCust == null || lgsCust.getId() == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入参数！");
        }
        String custName = lgsCust.getCustName();
        String custCode = lgsCust.getCustCode();
        String addr = lgsCust.getAddr();
        Integer areaId = lgsCust.getAreaId();

        if(custName == null || custName.equals("")){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入客户名称！");
        }
        if(!Pattern.matches(ValidUtil.WENZI_PATTERN,custName)){
            throw new BizzException(BizzException.error_code_invalid_params, "客户名称仅限汉字、数字、字母！");
        }
        if(custCode == null || custCode == ""){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入客户编码！");
        }
        if(!Pattern.matches(ValidUtil.PWS_PATTERN,custCode)){
            throw new BizzException(BizzException.error_code_invalid_params, "客户编码仅限数字、字母！");
        }
        if(addr == null || addr.equals("")){
            throw new BizzException(BizzException.error_code_invalid_params, "请输入详细地址！");
        }
        if(areaId == null){
            throw new BizzException(BizzException.error_code_invalid_params, "请选择区域！");
        }

        UserSession userSession = getUserSession(request);
        Integer companyId = userSession.getCompanyId();
        LgsCust cust = lgsCustService.testCode(companyId,custCode);
        if( cust != null && cust.getId() !=lgsCust.getId()){
            throw new BizzException(BizzException.error_code_invalid_params, "客户编码已存在！");
        }

        Boolean result = lgsCustService.update(lgsCust);
        return new HttpResponseBodyWrapper(result);
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/lgsCust/getLgsCust/{id}")
    public HttpResponseBodyWrapper getLgsCust(@PathVariable Integer id){
        Map<String, Object> map = new HashMap<>();
        LgsCust lgsCust = lgsCustService.getLgsCust(id);
        map.put("lgsCust", lgsCust);
        return new HttpResponseBodyWrapper(map);
    }

    /**************************** 进货网点 ****************************/

    /**
     * 进货网点获取分页列表信息
     * @param request
     * @param keyword
     * @param page
     * @return
     */
    @GetMapping("/cust/list")
    public ModelAndView queryCustList(HttpServletRequest request, String keyword, String custCode,String  mobile, Integer page){
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        page = (page == null || page < 1) ? 1 : page;
        keyword = StringUtils.trimToNull(keyword);
        custCode = StringUtils.trimToNull(custCode);
        mobile = StringUtils.trimToNull(mobile);
        Integer companyId = userSession.getCompanyId();
        Pageable<LgsCust> list = lgsCustService.queryCustList(companyId,keyword,custCode,mobile,page);

        model.put("custList", list);
        model.put("keyword", keyword);
        model.put("custCode", custCode);
        model.put("mobile", mobile);
        return new ModelAndView("cust_branch_list").addAllObjects(model);
    }

    /**
     * 进货记录
     * @param request
     * @param branchId
     * @param product
     * @param boxCode
     * @param scanTimeS
     * @param scanTimeE
     * @param page
     * @return
     */
    @GetMapping("/cust/scanList")
    public ModelAndView queryCustScanList(HttpServletRequest request, Integer branchId, String product, String boxCode,
                                          String scanTimeS, String scanTimeE, Integer page){
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        page = (page == null || page < 1) ? 1 : page;
        product = StringUtils.trimToNull(product);
        boxCode = StringUtils.trimToNull(boxCode);
        Integer companyId = userSession.getCompanyId();
        Pageable<Map<String,Object>> list = lgsCustService.queryCustScanList(companyId,branchId,product,boxCode,scanTimeS,scanTimeE,page);

        model.put("custScanList", list);
        model.put("product", product);
        model.put("boxCode", boxCode);
        model.put("branchId", branchId);
        model.put("scanTimeS", scanTimeS);
        model.put("scanTimeE", scanTimeE);
        return new ModelAndView("cust_scan_list").addAllObjects(model);
    }

}
